//
//  WYCategoryTableViewCell.m
//  Boss
//
//  Created by admin on 2018/6/23.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCategoryTableViewCell.h"
#import "WYSubCategoryCell.h"
#import "WYBusinessType.h"
#import "CJFourBusiness.h"
@interface WYCategoryTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;
@property (nonatomic,strong)NSArray *imageArray;
@property (nonatomic,strong)NSArray *nameArray;

@property (strong, nonatomic) IBOutlet UIImageView *adverImageView;
@end
@implementation WYCategoryTableViewCell
-(NSArray *)nameArray
{
    if (!_nameArray) {
        _nameArray = @[@"品牌加盟",@"渠道销售",@"BOSS人脉",@"BOSS头条"];
    }
    return _nameArray;
}

-(NSArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = @[@"business_brandPlus_icon",@"bussiness_ChannelSales_icon ",@"home_BOSS_Connection_ icon",@"home_BOSS_Headline_icon"];
    }
    return _imageArray;
}
-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self reloadCell];
    
    
}
- (void)reloadCell{
    [self.collectionView reloadData];
//    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@());
//    }];
//    self.collectionViewHeight.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
//    if (self.collectionViewHeight.constant !=self.collectionView.collectionViewLayout.collectionViewContentSize.height) {
//        if ([self.delegate respondsToSelector:@selector(didChangeHeight:)]) {
//            [self.delegate didChangeHeight:self];
//        }
//    }
    
    
    
    // [self.delegate didChangeCell:self];
}
-(void)setModel:(CJBrandAdvertising *)model
{
    _model = model;
    [self.adverImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",model.url]]];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpUI];
    self.adverImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAdver)];
    [self.adverImageView addGestureRecognizer:ges];
    self.adverImageView.clipsToBounds = YES;
    
    
    // Initialization code
}

-(void)clickAdver
{
    if ([self.delegate respondsToSelector:@selector(didAdverImageViewWithModel:)]) {
        [self.delegate didAdverImageViewWithModel:self.model];
    }
}


-(void)setUpUI
{
    self.collectionView.dataSource=self;
    self.collectionView.delegate = self;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-50)/4, (kScreenWidth-50)/4+5);
    //横向最小距离
    layout.minimumInteritemSpacing=10.0f;
    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYSubCategoryCell" bundle:nil] forCellWithReuseIdentifier:@"WYSubCategoryCell"];
    self.collectionView.bounces = NO;
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYSubCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYSubCategoryCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYSubCategoryCell" owner:nil options:nil].firstObject;
    }
//    WYBusinessType *businessType = self.dataArray[indexPath.row];
//    [cell.typeImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",businessType.url]] placeholderImage:[UIImage imageNamed:@"品牌专区_2"]];
//    NSLog(@"%@",businessType.name);
  //  cell.typeLabel.text = businessType.name;
    
    CJFourBusiness *businessType = self.dataArray[indexPath.row];
    [cell.typeImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",businessType.url]] placeholderImage:[UIImage imageNamed:@"品牌专区_2"]];
    NSLog(@"%@",businessType.name);
    cell.typeLabel.text = businessType.name;
//
//    cell.typeImage.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
//    cell.typeLabel.text = self.nameArray[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  //  CJFourBusiness *businessType = self.dataArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectCollectionViewcellWithIndex:WtihID:)]) {
        [self.delegate didSelectCollectionViewcellWithIndex:indexPath WtihID:@""];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
